﻿using CommunicationLib.Enums;
using PlcLib;
using PlcLib.Model;
using PLC通讯框架开发课程.Model;
using PLC通讯框架开发课程.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PLC通讯框架开发课程
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private byte[] GetBytes(int value) 
        { 
            byte[] result = new byte[4];
            result[0] = (byte)((value >> 24) & 0xff);
            result[1] = (byte)((value >> 16) & 0xff);
            result[2] = (byte)((value >> 8) & 0xff);
            result[3] = (byte)((value >> 0) & 0xff);
            return result;
        }


        private MainViewModel MainViewModel;
        private OrmonPlc OrmonPlc;
        public MainWindow()
        {
            InitializeComponent();

            MainViewModel = DataContext as MainViewModel;
            OrmonPlc = MainViewModel.OrmonPlc;

            var array = BitConverter.GetBytes(100).Reverse().ToArray();
            var array2 = this.GetBytes(100);
                //0x00 0x00 0x00 0x64 = 100
                //0x64 0x00 0x00 0x00 = 1677721600
        }

        

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Net net = new Net();
                net.LocalPort = 502;
                net.LocalIP = "127.0.0.1";

                net.IP = "192.168.1.101";
                net.Port = 502;
                OrmonPlc.Initialize(net);//初始化PLC
                OrmonPlc.Start();


                //OrmonPlc.Start(SetPraram);
            });
            
        }

        //开始PLC业务之前，先下发一些参数
        private void SetPraram()
        {
            Command command = new Command(OperatorMode.Write, AreaType.DM, DataType.WordHEX, 20, 100);

            OrmonPlc.Write(command, 789456);
        }

        private void Button_Click_Read(object sender, RoutedEventArgs e)
        {
            Command command = new Command(OperatorMode.Read, AreaType.DM, DataType.WordHEX, 2, 100);

            var result = OrmonPlc.Read(command);

            if (result.Success)
            {
                //todo  解析 result.ReceiveBuffer
                MessageBox.Show("读取成功");
            }
        }

        private void Button_Click_ReadAsync(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Read, AreaType.DM, DataType.WordHEX, 2, 100);

                var result = OrmonPlc.ReadAsync(command);

                if (result.Result.Success)
                {
                    //todo  解析 result.ReceiveBuffer
                    MessageBox.Show("异步读取成功");
                }
            });
            
        }

        private void Button_Click_Write(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Write, AreaType.DM, DataType.WordHEX, 1, 100);

                var result = OrmonPlc.Write(command, 1024);

                if (result.Success)
                {
                    MessageBox.Show("同步写入成功");
                }
            });            
        }

        private void Button_Click_WriteAsync(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Write, AreaType.DM, DataType.WordHEX, 1, 100);

                var result = OrmonPlc.Write(command, 1024);

                if (result.Success)
                {
                    MessageBox.Show("同步写入成功");
                }
            });
        }

        private void Button_Click_SendByte(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                byte[] array = new byte[] { 0x46, 0x49, 0x4E, 0x53 };

                var result = OrmonPlc.Send(array);

                if (result.Success)
                {
                    MessageBox.Show("发送byte成功");
                }
            });
        }

        private void Button_Click_SendByteAsync(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                byte[] array = new byte[] { 0x46, 0x49, 0x4E, 0x53 };

                var result = OrmonPlc.SendAsync(array);

                if (result.Result.Success)
                {
                    MessageBox.Show("异步发送byte成功");
                }
            });
           
        }

        private void Button_Click_SendByteArray(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Write, AreaType.DM, DataType.WordHEX, 10, 100);
                List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                var result = OrmonPlc.Write(command, list);

                if (result.Success)
                {
                    MessageBox.Show("同步写入int数组成功");
                }
            });
        }

        private void Button_Click_SendByteAsyncArray(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Write, AreaType.DM, DataType.WordHEX, 10, 100);
                List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                var result = OrmonPlc.WriteAsync(command, list);

                if (result.Result.Success)
                {
                    MessageBox.Show("同步写入int数组成功");
                }
            });
        }

        //同步读PLC泛型数据
        private void Button_Click_ReadPLC(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                Command command = new Command(OperatorMode.Read, AreaType.DM, DataType.WordHEX, 2, 100);

                var result = OrmonPlc.Read<float>(command);

                if (result.Success)
                {
                    Message message = new Message();
                    message.Content = $"同步读取泛型成功,读取值={result.Content},类型={result.Content.GetType().FullName}";
                    App.Current.Dispatcher.Invoke(() => MainViewModel.MessageList.Insert(0, message));

                }

                //4个字长回复 => 读两个int 或uint 
                //46 49 4E 53 00 00 00 1E 00 00 00 02 00 00 00 00 C0 00 02 00 34 00 00 01 00 FF 01 01 00 00  00 14 00 1E 01 02 03 04

                //2个字长回复 => 读1个int 或uint 或 float
                //46 49 4E 53 00 00 00 1A 00 00 00 02 00 00 00 00 C0 00 02 00 34 00 00 01 00 FF 01 01 00 00  00 14 00 1E 



                //if (result.Success)
                //{
                //    Message message = new Message();
                //    message.Content = $"同步读取泛型成功,读取值={result.Content},类型={result.Content.GetType().FullName}";
                //    App.Current.Dispatcher.Invoke(() => MainViewModel.MessageList.Insert(0, message));

                //}

                //var result2 = OrmonPlc.ReadAsync<int>(command);

                //if (result2.Result.Success)
                //{
                //    Message message = new Message();
                //    message.Content = $"异步读取泛型成功,读取值={result2.Result.Content}";
                //    MainViewModel.MessageList.Insert(0, message);
                //}

            });
        }

        //写泛型数据
        private void Button_Click_WritePLC(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                var f = BitConverter.GetBytes(10.10f);

                var r = BitConverter.ToSingle(f, 0); 

                Command command = new Command(OperatorMode.Write, AreaType.DM, DataType.WordHEX, 10, 100);
                List<float> list = new List<float>() { 255, 2.3f, 3.4f, 4.5f, 5, 6, 7, 8, 9, 10.10f };
                var result = OrmonPlc.Write(command, list.ToArray());
                if (result.Success)
                {
                    MessageBox.Show("同步写入short数组成功");
                }
            });
        }
    }
}
